package com.xindaoapp.happypet.view;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.utils.CommonUtil;



/*
 * 修改于：2013-2-28 17:03:35 修正 ListView item 点击响应失败！
 * 
 * @author Yichou
 */
public class PopMenu implements OnItemClickListener {
    public interface OnItemClickListener {
        public void onItemClick(int index);
    }

    private final ArrayList<String> itemList;
    private final Context context;
    private final PopupWindow popupWindow;
    // private final ListView listView;
    private OnItemClickListener listener;
    private final LayoutInflater inflater;


    public PopMenu(Context context) {
        this.context = context;

        itemList = new ArrayList<String>(5);

        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.popmenu1, null);
        view.findViewById(R.id.iv_jingxuan).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub

            }
        });
        view.findViewById(R.id.iv_zuixin).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub

            }
        });
        view.findViewById(R.id.iv_meitu).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub

            }
        });

        popupWindow = new PopupWindow(view, CommonUtil.dip2px(context, 90), // 这里宽度需要自己指定，使用
                                                                            // WRAP_CONTENT
                // 会很大
                CommonUtil.dip2px(context, 120));
        // 这个是为了点击“返回Back”也能使其消失，并且并不会影响你的背景（很神奇的）
        // popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

        // 设置FilterPopupWindow弹出窗体动画效果
        popupWindow.setAnimationStyle(R.style.PopupDownAnimation);
        // 实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0x80000000);
        // 设置FilterPopupWindow弹出窗体的背景
        popupWindow.setBackgroundDrawable(dw);
    }

    // @Override
    // public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    // if (listener != null) {
    // listener.onItemClick(position);
    // }
    // dismiss();
    // }

    // 设置菜单项点击监听器
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    // 批量添加菜单项
    public void addItems(String[] items) {
        for (String s : items)
            itemList.add(s);
    }

    // 单个添加菜单项
    public void addItem(String item) {
        itemList.add(item);
    }

    // 下拉式 弹出 pop菜单 parent 可以设置上拉式的形式，主要是看你相对于父控件的 坐标位置
    public void showAsDropDown(View parent) {
        popupWindow.showAsDropDown(parent, 0,
        // 保证尺寸是根据屏幕像素密度来的
                -5);

        // 使其聚集
        popupWindow.setFocusable(true);
        // 设置允许在外点击消失
        popupWindow.setOutsideTouchable(true);
        // 刷新状态
        popupWindow.update();
    }

    // 隐藏菜单
    public void dismiss() {
        popupWindow.dismiss();
    }

    // 适配器
    private final class PopAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return itemList.size();
        }

        @Override
        public Object getItem(int position) {
            return itemList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            // if (convertView == null) {
            // convertView = inflater.inflate(R.layout.popmenu1, null);
            // holder = new ViewHolder();
            // convertView.setTag(holder);
            // holder.groupItem = (TextView) convertView.findViewById(R.id.textView);
            // } else {
            // holder = (ViewHolder) convertView.getTag();
            // }
            // holder.groupItem.setText(itemList.get(position));

            TextView tv = new TextView(context);
            tv.setText(itemList.get(position));

            return tv;
        }

        private final class ViewHolder {
            TextView groupItem;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        // TODO Auto-generated method stub

    }
}
